package main

import (
	"fmt"
	"sync"

	"gitee.com/technology-to-share/concurrency-of-shared-variables/example3/bank"
)

func main() {
	for i := 0; i < 100000; i++ {
		test1()
	}
}

type Step struct{}

//将每个goroutine的操作串行化
func test1() {
	bank.Init()
	var wg sync.WaitGroup
	wg.Add(2)
	var a = make(chan *Step)
	var b = make(chan *Step)
	//A:
	go func(aStep chan<- *Step) {
		bank.Add(200)                    // A1
		fmt.Println("=", bank.Balance()) // A2
		aStep <- &Step{}
		wg.Done()
	}(a)

	// B:
	go func(bStep chan<- *Step, aStep <-chan *Step) {
		<-aStep
		bank.Add(100) // B
		// bStep <- &Step{} //如果有下一个协程，就要写
		wg.Done()
	}(b, a)

	wg.Wait()
	balance := bank.Balance()
	if balance != 300 {
		fmt.Println("====result====", balance)
	}
}
